import axios from 'axios';
import env from '../../build/env';
import baseurl from './baseurl'

let http = axios.create({
  baseURL: baseurl.url,
  timeout: 30000
});

if (env === 'production') {
  // request拦截器
  http.interceptors.request.use(config => {
      config.headers['X-Powered-By'] = 'huaban';
      if (sessionStorage.hbjwtauth) {
        config.headers['hbjwtauth'] = sessionStorage.hbjwtauth;
      }
      return config;
    },
    err => Promise.reject(err)
  );
  // response拦截器
  http.interceptors.response.use(
    response => response.data.code && response.data.code !== 'A001' ? Promise.reject(response.data) : Promise.resolve(response.data.data),
    error => Promise.reject(error)
  );
} else {
  // request拦截器
  http.interceptors.request.use(config => {
    config.headers['X-Powered-By'] = 'iViewAdmin';
    if (sessionStorage.hbjwtauth) {
      config.headers['hbjwtauth'] = sessionStorage.hbjwtauth;
    }
    return config;
  }, err => {
    console.log('request error:');
    console.log(err);
    return Promise.reject(err);
  });
  // response拦截器
  http.interceptors.response.use(
    response => {
      if (response.data.code && response.data.code !== 'A001') {
        console.log('%c>>>%c>>>%c>>>%c>>>%c>>>%c>>>%c>>>', 'color: #e74c3c', 'color: #e67e22', 'color: #f1c40f', 'color: #2ecc71', 'color: #1abc9c', 'color: #3498db', 'color: #9b59b6');
        console.log('出错了哦，' + response.data.errmsg);
        return Promise.reject(response.data);
      } else {
        console.log(response.config.url + '\nresponse data:');
        console.log(response.data.data);
        return Promise.resolve(response.data.data);
      }
    },
    error => {
      console.log('%c>>>%c>>>%c>>>%c>>>%c>>>%c>>>%c>>>', 'color: #e74c3c', 'color: #e67e22', 'color: #f1c40f', 'color: #2ecc71', 'color: #1abc9c', 'color: #3498db', 'color: #9b59b6');
      console.log(error);
      return Promise.reject(error);
    }
  );
}

// 导出各模块合并后的对象
export default http;
